package com.lzh.admin.controller;


import com.lzh.admin.entity.AdUser;
import com.lzh.admin.service.AdUserService;
import com.lzh.common.pojo.Result;
import com.lzh.core.controller.AbstractCoreController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
* <p>
* 管理员用户信息表 控制器</p>
* @author lzh
* @since 2021-06-23
*/
@RestController
@RequestMapping("/admin")
public class AdUserController extends AbstractCoreController<AdUser> {

    private AdUserService adUserService;

    //注入
    @Autowired
    public AdUserController(AdUserService adUserService) {
        super(adUserService);
        this.adUserService = adUserService;
    }

    @PostMapping("/register")
    public Result register(@RequestBody AdUser adUser) {
        boolean save = adUserService.saveUser(adUser);
        if (save) {
            return Result.ok("注册成功！");
        } else {
            return Result.error();
        }
    }

    @PostMapping("/login")
    public Result login(@RequestBody AdUser adUser) {
        if (StringUtils.isEmpty(adUser) || StringUtils.isEmpty(adUser.getName()) || StringUtils.isEmpty(adUser.getPassword())) {
            return Result.errorMessage("用户名或密码不能为空！");
        }
        Map<String,Object> userInfo = adUserService.login(adUser);
        if (userInfo == null) {
        return Result.errorMessage("登录失败！请检查用户名或密码是否正确！");
        }
        return Result.ok(userInfo);
    }

}

